/**
 * @ClassName: SysDiyProductPriceUtils
 * @Description: 自定次产品按会员等级设置价格
 * @author hl831 wzj
 * @date 2019-12-13
 */
package com.fzrskj.cloud.free.sys.utils;

import com.fzrskj.cloud.free.core.enums.PriceTypeEnum;
import com.fzrskj.cloud.free.core.utils.CloudContextUtils;
import com.fzrskj.cloud.free.dic.utils.DicParamUtils;
import com.fzrskj.cloud.free.sys.bean.SysPromotion;
import com.fzrskj.cloud.free.sys.dao.SysDiyProductPriceDao;
import com.fzrskj.cloud.free.sys.bean.SysDiyProductPriceBean;
import com.fzrskj.cloud.free.user.bean.UserAccount;
import com.fzrskj.cloud.free.user.dao.UserAccountDao;
import com.fzrskj.cloud.free.user.utils.UserAccountUtils;
import ms.core.consts.SysErr;
import ms.core.object.CObject;
import ms.core.tool.StrTool;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SysDiyProductPriceUtils extends CObject {
    //private declarations
    private SysDiyProductPriceDao dao = null;

    //constructor function to initialize database access object
    public SysDiyProductPriceUtils() {
        dao = new SysDiyProductPriceDao();
    }

    //to free resource
    @Override
    public void dispose() {
        if (dao != null) dao.dispose();
        dao = null;
        super.dispose();
    }

    //new record by map
    public int newSysDiyProductPrice(Map<String, Object> values) {
        int ret = dao.newSysDiyProductPrice(values);
        this.setErrCode(dao.getErrCode());
        return ret;
    }

    //delete record by id
    public int delSysDiyProductPrice(int id) {
        int ret = dao.delSysDiyProductPrice(id);
        this.setErrCode(dao.getErrCode());
        if (ret == 0) this.setErrCode(SysErr.SYS_DATA_NOT_FOUND);
        return ret;
    }

    //modify record by map
    public int modSysDiyProductPrice(Map<String, Object> values, Map<String, Object> cdt) {
        int ret = dao.modSysDiyProductPrice(values, cdt);
        this.setErrCode(dao.getErrCode());
        if (ret == 0) this.setErrCode(SysErr.SYS_DATA_NOT_FOUND);
        return ret;
    }

    //query record by id
    public SysDiyProductPriceBean querySysDiyProductPrice(int id) {
        SysDiyProductPriceBean ret = dao.querySysDiyProductPrice(id);
        String err = dao.getErrCode();
        this.setErrCode(err);
        if (err.equals(SysErr.CODE_SUCCESS) && ret == null) this.setErrCode(SysErr.SYS_DATA_NOT_FOUND);
        return ret;
    }

    //query record count by cdt
    public int querySysDiyProductPrice(Map<String, Object> cdt) {
        int ret = dao.querySysDiyProductPrice(cdt);
        this.setErrCode(dao.getErrCode());
        return ret;
    }

    //query record list by cdt
    public List<SysDiyProductPriceBean> querySysDiyProductPrice(Map<String, Object> cdt, String orderby, int page, int pagesize) {
        List<SysDiyProductPriceBean> rets = dao.querySysDiyProductPrice(cdt, orderby, page, pagesize);
        String err = dao.getErrCode();
        this.setErrCode(err);
        if (err.equals(SysErr.CODE_SUCCESS) && (rets == null || rets.size() == 0)) {
            this.setErrCode(SysErr.SYS_DATA_NOT_FOUND);
        }
        return rets;
    }

    //添加产品价格
    public boolean addSysDiyProductPrice(String priceItem, String productClass) {
        JSONArray json = JSONArray.fromObject(priceItem);
        if (json.size() <= 0) {
            return false;
        }

        for (int i = 0; i < json.size(); i++) {
            JSONObject job = json.getJSONObject(i);
            SysDiyProductPriceBean obj = new SysDiyProductPriceBean();
            obj.setProductClass(productClass);
            obj.setProductCode(job.getString("productCode"));
            obj.setChargeId(StrTool.strToIntDef(job.getString("chargeId"), 0));
            obj.setLevelCode(job.getString("levelCode"));

            obj.setPrice1(StrTool.strToIntDef(job.getString("price1"), -1));
            obj.setPrice2(StrTool.strToIntDef(job.getString("price2"), -1));
            obj.setPrice3(StrTool.strToIntDef(job.getString("price3"), -1));
            obj.setPrice4(StrTool.strToIntDef(job.getString("price4"), -1));
            obj.setPrice5(StrTool.strToIntDef(job.getString("price5"), -1));
            obj.setPrice6(StrTool.strToIntDef(job.getString("price6"), -1));
            obj.setPrice7(StrTool.strToIntDef(job.getString("price7"), -1));
            obj.setPrice8(StrTool.strToIntDef(job.getString("price8"), -1));
            obj.setPrice9(StrTool.strToIntDef(job.getString("price9"), -1));
            obj.setPrice10(StrTool.strToIntDef(job.getString("price10"), -1));
            obj.setPrice11(StrTool.strToIntDef(job.getString("price11"), -1));

            obj.setPricey1(StrTool.strToIntDef(job.getString("pricey1"), -1));
            obj.setPricey2(StrTool.strToIntDef(job.getString("pricey2"), -1));
            obj.setPricey3(StrTool.strToIntDef(job.getString("pricey3"), -1));

            dao.newSysDiyProductPrice(obj);
        }

        return true;
    }

    //删除产品促销
    public int delSysDiyProductPrice(Map<String, Object> cdt) {
        int i = dao.delSysDiyProductPrice(cdt);
        this.setErrCode(dao.getErrCode());
        return i;
    }

    //获取产品可购买时长
    public List<SysPromotion> getDiyProductBuyTime(Map<String, Object> cdt) {
        List<SysPromotion> promotionList = new ArrayList<>();
        do {

            UserAccountDao uaDao = new UserAccountDao();
            UserAccount ua = uaDao.getUserAccountWithoutAgent(UserAccountUtils.getTopAgentUserId(CloudContextUtils.getAgentId()));
            uaDao.dispose();

            String userLevel = "";
            if (ua != null) {
                userLevel = ua.getLevelCode();
            } else {
                userLevel = DicParamUtils.getParamValue("showPriceLevel");
            }

            String productCode = cdt.get("productCode").toString();
            Integer chargeId = Integer.valueOf(cdt.get("chargeId").toString());

            Map<String, Object> param = new HashMap<>();
            param.put("productCode", productCode);
            param.put("chargeId", chargeId);
            param.put("levelCode", userLevel);
            param.put("agentId", CloudContextUtils.DEF_AGENT_ID);

            SysDiyProductPriceBean view = dao.getyDiyProductPriceBuyTime(param);
            String code = dao.getErrCode();
            this.setErrCode(code);

            if (null == view) {
                break;
            }
            SysPromotion promotion;
            if (isSale(view.getPrice1())) {
                promotion = new SysPromotion();
                promotion.setProductCode(productCode);
                ;
                promotion.setChargeId(chargeId);
                promotion.setApplyTime(1);
                promotion.setApplyType(PriceTypeEnum.MONTH.getFlag());
                promotion.setSaleType(0);
                promotion.setAgentId(0);
                promotionList.add(promotion);
            }
            if (isSale(view.getPrice2())) {
                promotion = new SysPromotion();
                promotion.setProductCode(productCode);
                ;
                promotion.setChargeId(chargeId);
                promotion.setApplyTime(2);
                promotion.setApplyType(PriceTypeEnum.MONTH.getFlag());
                promotion.setSaleType(0);
                promotion.setAgentId(0);
                promotionList.add(promotion);
            }
            if (isSale(view.getPrice3())) {
                promotion = new SysPromotion();
                promotion.setProductCode(productCode);
                ;
                promotion.setChargeId(chargeId);
                promotion.setApplyTime(3);
                promotion.setApplyType(PriceTypeEnum.MONTH.getFlag());
                promotion.setSaleType(0);
                promotion.setAgentId(0);
                promotionList.add(promotion);
            }
            if (isSale(view.getPrice4())) {
                promotion = new SysPromotion();
                promotion.setProductCode(productCode);
                ;
                promotion.setChargeId(chargeId);
                promotion.setApplyTime(4);
                promotion.setApplyType(PriceTypeEnum.MONTH.getFlag());
                promotion.setSaleType(0);
                promotion.setAgentId(0);
                promotionList.add(promotion);
            }
            if (isSale(view.getPrice5())) {
                promotion = new SysPromotion();
                promotion.setProductCode(productCode);
                ;
                promotion.setChargeId(chargeId);
                promotion.setApplyTime(5);
                promotion.setApplyType(PriceTypeEnum.MONTH.getFlag());
                promotion.setSaleType(0);
                promotion.setAgentId(0);
                promotionList.add(promotion);
            }
            if (isSale(view.getPrice6())) {
                promotion = new SysPromotion();
                promotion.setProductCode(productCode);
                ;
                promotion.setChargeId(chargeId);
                promotion.setApplyTime(6);
                promotion.setApplyType(PriceTypeEnum.MONTH.getFlag());
                promotion.setSaleType(0);
                promotion.setAgentId(0);
                promotionList.add(promotion);
            }
            if (isSale(view.getPrice7())) {
                promotion = new SysPromotion();
                promotion.setProductCode(productCode);
                ;
                promotion.setChargeId(chargeId);
                promotion.setApplyTime(7);
                promotion.setApplyType(PriceTypeEnum.MONTH.getFlag());
                promotion.setSaleType(0);
                promotion.setAgentId(0);
                promotionList.add(promotion);
            }
            if (isSale(view.getPrice8())) {
                promotion = new SysPromotion();
                promotion.setProductCode(productCode);
                ;
                promotion.setChargeId(chargeId);
                promotion.setApplyTime(8);
                promotion.setApplyType(PriceTypeEnum.MONTH.getFlag());
                promotion.setSaleType(0);
                promotion.setAgentId(0);
                promotionList.add(promotion);
            }
            if (isSale(view.getPrice9())) {
                promotion = new SysPromotion();
                promotion.setProductCode(productCode);
                ;
                promotion.setChargeId(chargeId);
                promotion.setApplyTime(9);
                promotion.setApplyType(PriceTypeEnum.MONTH.getFlag());
                promotion.setSaleType(0);
                promotion.setAgentId(0);
                promotionList.add(promotion);
            }
            if (isSale(view.getPrice10())) {
                promotion = new SysPromotion();
                promotion.setProductCode(productCode);
                ;
                promotion.setChargeId(chargeId);
                promotion.setApplyTime(10);
                promotion.setApplyType(PriceTypeEnum.MONTH.getFlag());
                promotion.setSaleType(0);
                promotion.setAgentId(0);
                promotionList.add(promotion);
            }
            if (isSale(view.getPrice11())) {
                promotion = new SysPromotion();
                promotion.setProductCode(productCode);
                ;
                promotion.setChargeId(chargeId);
                promotion.setApplyTime(11);
                promotion.setApplyType(PriceTypeEnum.MONTH.getFlag());
                promotion.setSaleType(0);
                promotion.setAgentId(0);
                promotionList.add(promotion);
            }
            if (isSale(view.getPricey1())) {
                promotion = new SysPromotion();
                promotion.setProductCode(productCode);
                ;
                promotion.setChargeId(chargeId);
                promotion.setApplyTime(1);
                promotion.setApplyType(PriceTypeEnum.YEAR.getFlag());
                promotion.setSaleType(0);
                promotion.setAgentId(0);
                promotionList.add(promotion);
            }
            if (isSale(view.getPricey2())) {
                promotion = new SysPromotion();
                promotion.setProductCode(productCode);
                ;
                promotion.setChargeId(chargeId);
                promotion.setApplyTime(2);
                promotion.setApplyType(PriceTypeEnum.YEAR.getFlag());
                promotion.setSaleType(0);
                promotion.setAgentId(0);
                promotionList.add(promotion);
            }
            if (isSale(view.getPricey3())) {
                promotion = new SysPromotion();
                promotion.setProductCode(productCode);
                ;
                promotion.setChargeId(chargeId);
                promotion.setApplyTime(3);
                promotion.setApplyType(PriceTypeEnum.YEAR.getFlag());
                promotion.setSaleType(0);
                promotion.setAgentId(0);
                promotionList.add(promotion);
            }

        } while (false);
        return promotionList;
    }

    //获取自定义产品按会员等级设置价格
    public int getDomainProductPrice(int applyTime, String priceType, Map<String, Object> cdt) {
        //是否允许获取按会员等级设置价格
        int price = -1;
        String isYear = "";
        if (PriceTypeEnum.YEAR.getFlag().equals(priceType)) {
            isYear = "y";
        }
        String year = isYear + applyTime;
        cdt.put("agentId", CloudContextUtils.DEF_AGENT_ID);
        price = dao.getSysDiyProductPrice(year, cdt);
        String code = dao.getErrCode();
        this.setErrCode(code);
        dao.dispose();
        return price >= 0 ? price : -1;
    }

    private Boolean isSale(Integer price) {
        if (price != null && price != 0) {
            return true;
        }
        return false;
    }



}